package top.revery.task.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RestController;
import top.revery.api.api.task.ITaskAppealApi;
import top.revery.api.config.UserPageData;

import top.revery.api.entity.task.BaseTaskAppeal;
import top.revery.task.dto.TaskAppealDto;
import top.revery.task.service.TaskAppealService;
import top.revery.task.util.FeignServiceMapping;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author denghao
 * @title
 * @package
 * @date 2019/12/19 13:55
 */
@RestController
public class TaskAppealController implements ITaskAppealApi {
    @Autowired
    private TaskAppealDto taskAppealDto;
    @Autowired
    private TaskAppealService taskAppealService;

    @Override
    public BaseTaskAppeal save(Long taskId, String content, Long id) throws Exception {
        BaseTaskAppeal baseTaskAppeal = new BaseTaskAppeal();
        baseTaskAppeal.setAppealT(new Timestamp(new Date().getTime()));
        baseTaskAppeal.setContent(content);
        baseTaskAppeal.setTaskId(taskId);
        baseTaskAppeal.setUserId(id);
        baseTaskAppeal.setStatus(0);
        return taskAppealDto.save(baseTaskAppeal);
    }

    @Override
    public UserPageData getList(int pageNum, int pageSize, Map map) {

        Page<BaseTaskAppeal> pageList = taskAppealService.findPage(pageNum, pageSize, map);
        UserPageData userPageData = new UserPageData();
        List<BaseTaskAppeal> content = pageList.getContent();
        userPageData.list = content;
        userPageData.rows = pageList.getTotalElements();
        return userPageData;
    }

    @Override
    public void updateStatus(BaseTaskAppeal baseTaskAppeal) throws Exception{

            if (baseTaskAppeal.getStatus() == 1) {
                throw new Exception("此申诉已经审核！");
            }
            baseTaskAppeal.setStatus(1);
            taskAppealDto.save(baseTaskAppeal);

    }

    @Override
    public Object getHome() {
        UserPageData list = FeignServiceMapping.taskUserService.getList(1, 10);
        System.out.println(list.list);
        return list.list;
    }
}
